import pygame, numpy

import colors

def drawPolygon(vertices, color, filled=True):
    ''' wrapper fucntion for graphics '''
    pygame.draw.

class gfxLayer:
    def __init__(_, thescreen, layersize, blocksize):
        _.origin = [0, 0]
        _.size = layersize
        _.blocksize = blocksize

        # FIXME: replace this eventually with texture magic
        _.font = pygame.font.Font('arialuni.ttf', _.blocksize[0])

        _.screen = thescreen

        _.charset = {}

    def addChar(_, newchar, name):
        charpic = _.font.render(newchar, False, [255,255,255], [0,0,0])
        charpic = charpic.convert(8) # convert to an 8-bit surface

        # clip the character to the size we want
        #charpic = pygame.transform.chop(charpic,
        #    pygame.Rect((0,0), (charpic.get_width()-1, charpic.get_height()) ))

        _.charset[name] = charpic

        #if GLOBALS.debug is True:
        #   print "Loaded " + name + " : " + newchar

    def drawChar(_, char, pos, fcolor, bcolor):
        # if, for some reason, we got a string, cut it down to the first char
        char = char[0]
        
        # FIXME: currently we can't draw off the screen
        # but maybe we should be able to for sake of future optimisations
        if pos[0] > _.size[0] or pos[0] < 0:
                return
        if pos[1] > _.size[1] or pos[1] < 0:
                return

        # draw the character on a surface
        text = _.font.render(char, True, fcolor, bcolor)

        
        textpos = text.get_rect( left=int(pos[0]*_.blocksize[0]) + _.origin[0],
                                 top=int(pos[1]*_.blocksize[1]) + _.origin[1] )

        return _.screen.blit(text, textpos)

    def drawSurf(_, surfname, pos, fcolor, bcolor):
        surf = _.charset[surfname]

        # replace all black with the background color
        surf.set_palette_at(0, bcolor)
        surf.set_palette_at(1, fcolor)
        
        # replace all whitish colors with the foreground color... right?

        if surf is not None:
            textpos = pygame.Rect( int(pos[0]*_.blocksize[0]) + _.origin[0],
                            int(pos[1]*_.blocksize[1]) + _.origin[1],
                            _.blocksize[0], _.blocksize[1])
            
            #textpos = surf.get_rect( left=int(pos[0]*_.blocksize[0]) + _.origin[0],
            #                         top=int(pos[1]*_.blocksize[1]) + _.origin[1])
            return _.screen.blit(surf, textpos)
        else:
            return

    def drawString(_, string, pos, fcolor, bcolor):
        for char in string:
            _.drawChar(char, pos, fcolor, bcolor)
            pos[0] += 1

    def drawBox(_, char, pos, widthheight, fcolor, bcolor, filled=False):
        if filled is False:
            # draw top row
            for col in range(0, widthheight[0]):
                mypos = [col, 0]
                
                # add offset
                mypos[0] += pos[0]
                mypos[1] += pos[1]

                _.drawSurf(char, mypos, fcolor, bcolor)
            # draw left col
            for row in range(1, widthheight[1]-1):
                mypos = [0, row]
                
                # add offset
                mypos[0] += pos[0]
                mypos[1] += pos[1]

                _.drawSurf(char, mypos, fcolor, bcolor)
            # draw right col
            for row in range(1, widthheight[1]-1):
                mypos = [widthheight[0]-1, row]
                
                # add offset
                mypos[0] += pos[0]
                mypos[1] += pos[1]

                _.drawSurf(char, mypos, fcolor, bcolor)
            # draw bottom row
            for col in range(0, widthheight[0]):
                mypos = [col, widthheight[1]-1]
                
                # add offset
                mypos[0] += pos[0]
                mypos[1] += pos[1]

                _.drawSurf(char, mypos, fcolor, bcolor)

        else:            
            for row in range(0, widthheight[0]):
                for col in range(0, widthheight[1]):
                    _.drawSurf(char, [row + pos[0], col + pos[1]], fcolor, bcolor)
                    

    

    def blitTo(_, mainscreen):
        # FIXME : make the position something else
        _.screen.blit(mainscreen, [0,0])



class Map:
    def __init__(_, mapsize, gfxhandler):
        # set up map array
        _.data = numpy.zeros( ( mapsize[0], mapsize[1] ) )#, dtype=numpy.int8)
        #_.data.reshape(mapsize[0], mapsize[1])

        # FIXME : it's all just graaaaaass!
        #for row in range(0, mapsize[0]):
        #    for col in range(0, mapsize[1]):
        #        _.data[row, col] = 1

        _.gfxhandler = gfxhandler
    
    def drawPortion(_, origin, widthheight):
        #first, slice the portion of the data array we want
        #absolute = [ origin[0] + widthheight[0], origin[1] + widthheight[1] ]
        #portion = _.data[ origin[0]:absolute[0], origin[1]:absolute[1] ]
        
        for row in range(origin[1], origin[1] + widthheight[1]):
            for col in range(origin[0], origin[0] + widthheight[0]):
                # FIXME : convert data from int8 into the char we want to
                #   draw
                myint = _.data[row, col]
                                    
                _.gfxhandler.drawSurf("grass", [row, col], colors.black, colors.green)
                

